package com.stars.questions.commonQuestion.config.controller;

import com.alibaba.fastjson2.JSONObject;
import com.stars.base.constants.CommonNumberConstants;
import com.stars.base.entity.CommonDTO;
import com.stars.base.entity.Result;
import com.stars.questions.commonQuestion.config.entity.QuestionsCommonConfig;
import com.stars.questions.commonQuestion.config.service.QuestionsCommonConfigService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/questions/common/config")
public class QuestionsCommonConfigController {

    private final QuestionsCommonConfigService configService;

    public QuestionsCommonConfigController(QuestionsCommonConfigService configService) {
        this.configService = configService;
    }

    @RequestMapping("/list")
    public Result getListData(@RequestBody CommonDTO dto) {

        return Result.success(configService.configList(dto));
    }

    /**
     * 根据问卷ID获取问卷实体
     * @author : moon
     * @date :  2024/6/29 17:16
     * @param: id 主键
     * @return: com.stars.base.entity.Result
     */
    @RequestMapping("/data")
    public Result getConfigFormData(String id){
        QuestionsCommonConfig questionData = configService.loadQuestionData(id);
        return Result.success(questionData);
    }

    /**
     * 保存问卷
     * @author : moon
     * @date :  2024/6/30 10:40
     * @param: bean 问卷调查实体Bean
     * @return: com.stars.base.entity.Result
     */
    @PostMapping("/save")
    public Result ConfigSave(@RequestBody QuestionsCommonConfig bean){
        return Result.result(configService.formSave(bean));
    }

    /**
     * 删除调查问卷
     * @author : moon
     * @date :  2024/6/30 10:41
     * @param: id 问卷id
     * @return: com.stars.base.entity.Result
     */
    @PostMapping("/delRecord")
    public Result delRecord(String id) {
        return Result.result(configService.delRecord(id));
    }

    /**
     * 发布/取消发布问卷
     * @author : moon
     * @date :  2024/6/30 10:45
     * @param: id 问卷id
     * @param: type 1发布 -1取消发布
     * @return: com.stars.base.entity.Result
     */
    @PostMapping("/publish")
    public Result publish(String id, String type){
        if(CommonNumberConstants.ONE_STR.equals(type)){
            JSONObject result = configService.publish(id);
            if(result.getBoolean("result")){
                return Result.success();
            }
            return Result.error(result.getString("msg"));
        }else if(CommonNumberConstants.MINUS_ONE_STR.equals(type)){
            return Result.result(configService.unPublish(id));
        }
        return  Result.error("未知操作类型");
    }

    /**
     * 子表删除
     * @author : moon
     * @date :  2024/6/30 11:58
     * @param: subId 子表id
     * @return: com.stars.base.entity.Result
     */
    @PostMapping("/deleteSub")
    public Result deleteSub(String subId){
        return Result.result(configService.deleteSub(subId));
    }
}
